import 'package:flutter/material.dart';

import 'package:flutter_redux/flutter_redux.dart';
import 'package:my_flutter_github/common/Config.dart';
import 'package:my_flutter_github/common/LocalStorage.dart';
import 'package:my_flutter_github/common/dao/UserDao.dart';
import 'package:my_flutter_github/common/db/SqlManager.dart';
import 'package:my_flutter_github/utils/NavigatorUtils.dart';
import 'package:my_flutter_github/widgets/FlexButton.dart';
import 'package:redux/redux.dart';
import 'package:my_flutter_github/utils/CommonUtils.dart';

import 'package:my_flutter_github/MyStyle.dart';

import 'package:my_flutter_github/redux/MyState.dart';
import 'package:my_flutter_github/model/User.dart';

import 'package:get_version/get_version.dart';

class HomeDrawer extends StatelessWidget {
  //关于
  _showAboutDialog(BuildContext context, String versionName) {
    versionName ??= "NULL";
    showDialog(
      context: context,
      builder: (BuildContext context) => new AboutDialog(
            applicationName: "MyFirstApp",
            applicationVersion: "当前版本:" + versionName,
            applicationIcon: new Image(
              image: new AssetImage(MyIcons.DEFAULT_USER_ICON),
              width: 50.0,
              height: 50.0,
            ),
            applicationLegalese: "https://www.baidu.com",
          ),
    );
  }

  //主题
  _showThemeDialog(BuildContext context, Store store) {
    List<String> list = ["默认主题", "主题1", "主题2", "主题3", "主题4", "主题5", "主题6"];
    CommonUtils.showCommitOptionDialog(
        context: context,
        commitList: list,
        onItemClick: (index) {
          print("主题切换");
          //点击切换主题
          CommonUtils.pushTheme(store, index);
          LocalStorage.save(Config.THEME_COLOR, index.toString());
        },
        colorList: CommonUtils.getThemeListColor());
  }

  //语言切换
  _showLanguageDialog(BuildContext context, Store store) {
    List<String> list = ["默认", "中文", "英文"];
    CommonUtils.showCommitOptionDialog(
        context: context,
        commitList: list,
        onItemClick: (index) {
          print("语言切换");
//      CommonUtils.changeLocale(store, index);
//      LocalStorage.save(Config.LOCALE, index.toString());
        },
        height: 150.0);
  }

  @override
  Widget build(BuildContext context) {
    return new StoreBuilder<MyState>(
      builder: (context, store) {
        User user = store.state.userInfo;
        return new Drawer(
          child: new Container(
            ///默认背景
            color: store.state.themeData.primaryColor,
            child: new SingleChildScrollView(
              child: new Container(
                constraints: new BoxConstraints(
                    minHeight: MediaQuery.of(context).size.height),
                child: new Material(
                  color: Colors.white,
                  child: new Column(
                    children: <Widget>[
                      //Material内置的头像控件
                      new UserAccountsDrawerHeader(
                        //用户名
                        accountName: new Text(
                          user.login ?? "---",
                          style: MyConstants.largeTextWhite,
                        ),
                        accountEmail: new Text(
                          user.email ?? user.name ?? "---",
                          style: MyConstants.normalTextWhite,
                        ),
                        //用户头像
                        currentAccountPicture: new GestureDetector(
                          onTap: () {
                            print("AccountPicture onTop");
                          },
                          child: new CircleAvatar(
                            //圆形图标控件
                            backgroundImage: new NetworkImage(
                                user.avatar_url ?? MyIcons.DEFAULT_REMOTE_PIC),
                          ),
                        ),
                        decoration: new BoxDecoration(
                          //用一个BoxDecoration装饰器提供背景图片
                          color: store.state.themeData.primaryColor,
                        ),
                      ),

                      ///问题反馈
                      new ListTile(
                        title: new Text("问题反馈", style: MyConstants.normalText,),
                        onTap: (){
                          String result = "";
                          CommonUtils.showEditDialog(
                            context: context,
                            dialogTitle: "问题反馈",
                            onTitleChanged: (title) {
                              print("问题反馈 on title change:" + title);
                            },
                            onContentChanged: (content) {
                              result = content;
                              print("问题反馈 on content change:" + content);
                            },
                            onPressed: (){
                              print("问题反馈 on onPressed");
                              if(result.isEmpty) {
                                return;
                              }
                            },
                            titleController: new TextEditingController(),
                            valueController: new TextEditingController(),
                            needTitle: true,
                          );
                        },
                      ),

                      new Divider(height: 1.0,),

                      ///阅读历史
                      new ListTile(
                        title: new Text("阅读历史", style: MyConstants.normalText,),
                        onTap: (){
                          NavigatorUtils.gotoCommonList(context, "阅读历史", "repository", "history",
                              userName: "", reposName: "");
                        },
                      ),

                      new Divider(height: 1.0,),

                      ///个人信息
                      new ListTile(
                        title: new Text("个人信息", style: MyConstants.normalText,),
                        onTap: (){
                          NavigatorUtils.gotoUserProfileInfo(context);
                        },
                      ),

                      new Divider(height: 1.0,),

                      ///切换主题
                      new ListTile(
                        title: new Text("切换主题", style: MyConstants.normalText,),
                        onTap: (){
                          _showThemeDialog(context, store);
                        },
                      ),

                      new Divider(height: 1.0,),

                      ///语言切换
                      new ListTile(
                        title: new Text("语言切换", style: MyConstants.normalText,),
                        onTap: (){

                        },
                      ),

                      new Divider(height: 1.0,),

                      ///检测更新
                      new ListTile(
                        title: new Text("检测更新", style: MyConstants.normalText,),
                        onTap: (){

                        },
                      ),

                      new Divider(height: 1.0,),

                      ///关于
                      new ListTile(
                        title: new Text("关于", style: MyConstants.normalText,),
                        onTap: (){
                          GetVersion.projectVersion.then((String version) {
                            print("version:$version");
                            _showAboutDialog(context, version);
                          });
                        },
                      ),

                      new Divider(height: 1.0,),

                      //退出登录按钮
                      new ListTile(
                        title: new FlexButton(
                          text: "退出登录",
                          color: Colors.redAccent,
                          textColor: Colors.white,
                          onPress: (){
                            print("退出登录");
                            UserDao.clearAll(store);
//                            EventDao.clearEvent(store);
                            SqlManager.close();
                            NavigatorUtils.goLogin(context);
                          },
                        ),
                      ),
                    ],
                  ),
                ),
              ),
            ),
          ),
        );
      },
    );
  }
}
